package com.leessy.r229.utils;

import android.text.TextUtils;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * get dev mac
 */
public final class MacAddressUtils {
    public static String getEth0Mac() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
            pp.waitFor();
            input.close();
            ir.close();
        } catch (Exception ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        if (TextUtils.isEmpty(macSerial)) {
            return getWlanMac();
        }
        if (macSerial == null) return "";
        return macSerial;
    }


    //获取wlan网卡mac地址
    public static String getWlanMac() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
            pp.waitFor();
            ir.close();
            input.close();
        } catch (Exception ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }
}
